//
//  VideoViewController.m
//  FileLockerPro
//
//  Created by SDSL-3 on 7/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VideoViewController.h"
#import "AssetManager.h"
#import <MobileCoreServices/UTCoreTypes.h>
#import "PTDemoViewController.h"
#import "DeleteExportViewController.h"

@implementation VideoViewController
@synthesize appdelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
    self.appdelegate = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



#pragma mark tableview

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }
    else if(section == 1){
        return 1;
    }
    else if(section == 2){
        return 2;
    }
    
    return 0;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"Import";
    }
    else if(section == 1){
        return @"View";
    }
    else if(section == 2){
        return @"Manage";
    }
    
    return @"Untitled";
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"menu_cell_id";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            cell.textLabel.text = @"Record A Video";
        }
        else if(indexPath.row == 1){
            cell.textLabel.text = @"From Video Library";
        }
    }
    else if(indexPath.section == 1){
        if(indexPath.row == 0){
            cell.textLabel.text = @"My Videos";
        }
    }
    else if(indexPath.section == 2){
        if(indexPath.row == 0){
            cell.textLabel.text = @"Delete";
        }
        else if(indexPath.row == 1)
        {
            cell.textLabel.text = @"Export";
        }
    }
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0){
        if(appdelegate.imagePickerController == nil){
            UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
            appdelegate.imagePickerController = ipc;
            [ipc release];
        }
        appdelegate.imagePickerController.delegate = self;
        appdelegate.imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil] autorelease];
        if(indexPath.row == 0){
            fromCamera = YES;
            //cell.textLabel.text = @"Take a photo";
            appdelegate.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        else if(indexPath.row == 1){
            fromCamera = NO;
            //cell.textLabel.text = @"From Photo Library";
            appdelegate.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        
        [self presentModalViewController:appdelegate.imagePickerController animated:YES];
    }
    else if(indexPath.section == 1){
        if(indexPath.row == 0){
            //cell.textLabel.text = @"My Videos"; 
        
            AssetManager *am = [[AssetManager alloc] init];
            NSMutableArray *allvideos = [am getAllVideoPaths];
            [am release];
            
            if(allvideos != nil && [allvideos count] > 0){
                PTDemoViewController *vvc = [[PTDemoViewController alloc] init];
                vvc.videos = allvideos;
                [self.navigationController pushViewController:vvc animated:YES];
                [vvc release];
            }   
            else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"You do not have any videos yet!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alert show];
                [alert release];
            }
        }
    }
    else if(indexPath.section == 2){
        if(indexPath.row == 0 || indexPath.row == 1){
            DeleteExportViewController *devc = [[DeleteExportViewController alloc] init];
            devc.forImage = NO;
            AssetManager *am = [[AssetManager alloc] init];
            devc.items = [am getAllVideoPaths];
            [am release];
            [self.navigationController pushViewController:devc animated:YES];
            [devc release];
        }
    }
    
}
    
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
    if([mediaType isEqualToString:@"public.movie"]){
        NSURL *url =  [info objectForKey:UIImagePickerControllerMediaURL];
        //NSLog(@"> %@", [url absoluteString]);
        
        AssetManager *am = [[AssetManager alloc] init];
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"d MMM yyyy, hh:mm aaa"];
        
        NSString *videoName = [NSString stringWithFormat:@"%@.MOV", [df stringFromDate:[NSDate date]]];
        [df release];
        [am saveVideo:url WithName:videoName];
        [am release];
        
        if(fromCamera == NO){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Transfer Success!" message:@"Please make sure you delete the original Video from the public Video Library.\n\nDue to security restrictions, Apple doesn't allow Apps to delete Videos from the public Video Library." delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
        
        
    }
    
    [appdelegate.imagePickerController dismissModalViewControllerAnimated:YES];
    appdelegate.imagePickerController.delegate = nil;
    
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    [image retain];
    [appdelegate.imagePickerController dismissModalViewControllerAnimated:YES];
    appdelegate.imagePickerController.delegate = nil;
    
    AssetManager *am = [[AssetManager alloc] init];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"d MMM yyyy, hh:mm aaa"];
    
    NSString *imageName = [NSString stringWithFormat:@"%@.jpg", [df stringFromDate:[NSDate date]]];
    [df release];
    [am saveImage:image WithName:imageName];
    [image release];
    [am release];
    
    if(fromCamera == NO){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image Transfer Success!" message:@"Please make sure you delete the original Image from the public Photo Library.\n\nDue to security restrictions, Apple doesn't allow Apps to delete images from the public Photo Library." delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}


@end
